/**
  ******************************************************************************
  * @file    String.c 
  * @author  Thongnh
  * @version V1.0
  * @date    23/10/2012
  ******************************************************************************
**/

/* Includes ------------------------------------------------------------------*/
#include "StrLib.h"


void cStrLib::Strcpy(U8* dst, const U8* src, const U16 len)
{
    U16 i;
    for (i=0; i<len; i++)
    {
        dst[i] = src[i];
    }
}

bool cStrLib::Strcmp(const U8* iData1, const U8* iData2, const U16 len)
{
    U16 i;
    for (i=0; i<len; i++)
    {
        if (iData1[i] != iData2[i]) return (false);
    }

    return (true);
}

bool cStrLib::StrCmpWithConst(const U8* iData1, const U8 iConst, const U16 len)
{
    U16 i;
    for (i=0; i<len; i++)
    {
        if (iData1[i] != iConst) return (false);
    }

    return (true);
}

U16 cStrLib::StrToWord( U8* pStr )
{
    return ( (((U16)pStr[0])<<8) | ((U16)pStr[1]) );
}


void cStrLib::WordToDecStr( U8* pStr, U16 iVal )
{
    U16 valNgan     = iVal % 10000;
    U16 valTram     = valNgan % 1000;
    U16 valChuc     = valTram % 100;

    pStr[0] = (valNgan / 1000) + 0x30; 
    pStr[1] = (valTram / 100) + 0x30;
    pStr[2] = (valChuc / 10) + 0x30;
    pStr[3] = (valChuc % 10) + 0x30;
}


U8 cStrLib::HexToAscii(U8 iHex)
{
    U8 temp = iHex & 0x0F;
    if (9 < temp) temp += 7;
    return (temp + '0');
}


U8 cStrLib::AsciiToHex(U8 iAscii)
{
    U8 temp = iAscii & 0x7F;
    if ('9' < temp) temp += 9;
    return (temp & 0x0F);
}


U8 cStrLib::HexToDec(U8 iHex)
{
    return ((iHex/10)<<4 | (iHex%10));
}


U8 cStrLib::DecToHex(U8 iDec)
{
    return ((iDec>>4)*10 + (iDec & 0x0F));
}


void cStrLib::ByteToHexStr( U8* pStr, U8 iHex )
{
    pStr[0] = HexToAscii(iHex >>4);
    pStr[1] = HexToAscii(iHex & 0x0F);
}


void cStrLib::StrToStrDec(U8 *pDec, U8 *pStr, U8 iLen)
{
    U8 k = 0;
    for (U8 i=0; i<iLen/2; i++)
    {
        U8 high = AsciiToHex(pStr[k++]);
        U8 low = AsciiToHex(pStr[k++]);
        pDec[i] = high * 10 + low;
    }
}


U8 cStrLib::StrDecToByte(U8 *pStr)
{
    return (AsciiToHex(pStr[0]) * 10 + AsciiToHex(pStr[1]));
}


void cStrLib::ByteToStrDec(U8 *pStr, U8 iByte)
{
    pStr[0] = HexToAscii(iByte/10);
    pStr[1] = HexToAscii(iByte%10);
}


U16 cStrLib::StrDecToWord(U8 *pStr)
{
    return (AsciiToHex(pStr[0])*1000 + AsciiToHex(pStr[1])*100 + AsciiToHex(pStr[2]) * 10 + AsciiToHex(pStr[3]));
}

//
//void cStrLib::WordToStrDec(U8 *pStr, U16 iWord)
//{
//    pStr[0] = HexToAscii(iWord / 1000);
//    return (AsciiToHex(pStr[0])*1000 + AsciiToHex(pStr[1])*100 + AsciiToHex(pStr[2]) * 10 + AsciiToHex(pStr[3]));
//}


void cStrLib::StrDecToStr(U8 *pStr, U8 *pDec, U8 iLen)
{
    U8 k = 0;
    for (U8 i=0; i<iLen; i++)
    {
        pStr[k++] = HexToAscii(pDec[i] / 10);
        pStr[k++] = HexToAscii(pDec[i] % 10);
    }
}


void cStrLib::StrToStrHex(U8 *pHex, U8 *pStr, U8 iLen)
{
    U8 k = 0;
    for (U8 i=0; i<iLen/2; i++)
    {
        U8 high = AsciiToHex(pStr[k++]);
        U8 low = AsciiToHex(pStr[k++]);
        pHex[i] = high<<4 | low;
    }
}


void cStrLib::StrHexToStr(U8 *pStr, U8 *pHex, U8 iLen)
{
    U8 k = 0;
    for (U8 i=0; i<iLen; i++)
    {
        pStr[k++] = HexToAscii(pHex[i] >> 4);
        pStr[k++] = HexToAscii(pHex[i] & 0x0F);
    }
}


U8 cStrLib::GetHigh16( U16 iVal )
{
    return ((U8)(iVal>>8));
}


U8 cStrLib::GetLow16( U16 iVal )
{
    return ((U8)iVal);
}

void cStrLib::DelayUs(U16 us)
{
    U8 cnt;
    while (us)
    {
        cnt = 4;
        while (--cnt);
        --us;
    }
}

void cStrLib::DelayMs(U16 ms)
{
    while (ms)
    {
        DelayUs(1000);
        --ms;
    }
}

